package com.javajun.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javajun.entity.Admin;
import com.javajun.service.AdminService;

/**
 * @ClassName: LoginController
 * @Description: 登录的控制器
 * @author: xlm
 * @date: 2018年10月15日
 */
@Controller
@RequestMapping(value="/")
public class LoginController {

	@Autowired
	private AdminService adminService;
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public String Login(ModelMap map, HttpSession session, String username, String password) {
		Boolean b = false;
		String ViewUrl = null;
		Admin admin = adminService.findByUsernameAndPassword(username, password);
		if(admin != null) {
			b = true;
		}
		if(b) {
			session.removeAttribute("user");
			session.setAttribute("user", admin);
			ViewUrl = "redirect:admin/";
		} else {
			session.removeAttribute("user");
			ViewUrl = "redirect:/";
		}
		return ViewUrl;
	}
	
	@RequestMapping(value="/", method=RequestMethod.GET)
	public String Login() {
		return "login";
	}
}
